<template>
	<view class="content">
		<!-- 轮播图 -->
		<view class="swipe">
			<u-swiper :list="list" border-radius="0" height="360"></u-swiper>
		</view>
		<!-- 报修类目 -->
		<view class="leimu">
			<view class="leimu-title">
				<text>报修类目</text>
			</view>
			<!-- <view class="leimu-list">
				<view class="leimu-li" v-for="(item,index) in leumuList" :key="index" @click="clcikNavigator(item,index)">
					<image :src="item.logo_image" mode=""></image>
					<text>{{item.name}}</text>
				</view>
			</view> -->
			<view class="u-menu-wrap">
				<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
					<view v-for="(item,index) in leumuList" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
					 :data-current="index" @tap.stop="swichMenu(index)">
						<text class="u-line-1">{{item.name}}</text>
					</view>
				</scroll-view>
				<block v-for="(item,index) in leumuList" :key="index">
					<scroll-view scroll-y class="right-box" v-if="current==index">
						<view class="page-view">
							<view class="class-item">
								<!-- <view class="item-title">
									<text>{{item.name}}</text>
								</view> -->
								<view class="item-container">
									<view class="thumb-box" v-for="(item1, index1) in item.child" :key="index1" @click="clcikNavigator(item1,index1)">
										<image class="item-menu-image" :src="item1.logo_image" mode=""></image>
										<view class="item-menu-name">{{item1.name}}</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</block>
			</view>
			
		</view>
		<u-tabbar :list="tabbar" height='130rpx' active-color='#0055B0' inactive-color='#7F8699'></u-tabbar>
		<u-modal v-model="show" :content="content" :confirm-text="confirm_text" show-cancel-button cancel-text="取消" @confirm="qdinfo()"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [
				],
				leumuList:[],
				// tabbar:'',
				
				scrollTop: 0, //tab标题的滚动条位置
				oldScrollTop: 0,
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				itemId: '', // 栏目右边scroll-view用于滚动的id
				tabbar: [],
				menuItemPos: [],
				arr: [],
				scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
				timer: null, // 定时器
				show:false,//弹窗
				content:'您未完善身份信息',
				confirm_text:"去完善",
				
				
			}
		},
		onLoad() {
			// this.renew()
			this.tabbar = [
				{
					iconPath: "/static/home.png",
					selectedIconPath: "/static/home-a.png",
					text: '首页',					
					pagePath: "/pages/user/index/index"
				},
				{
					iconPath: "/static/my.png",
					selectedIconPath: "/static/my-a.png",
					text: '我的',
					pagePath: "/pages/user/my/my"
				}
			]
			this.getBannerList()
			this.getClassi()
			if(uni.getStorageSync('servToken')){
				uni.switchTab({
					url:'/pages/service/index/index'
				})
			}
			if(uni.getStorageSync('headToken')){
				uni.switchTab({
					url:'/pages/head/head_main/head_main'
				})
			}
			if(uni.getStorageSync('xiheadToken')){
				uni.switchTab({
					url:'/pages/xihead/xihead_main/xihead_main'
				})
			}
			this.checkinfo()
			
		},
		methods: {
			renew(){
				const updateManager = uni.getUpdateManager();
				
				updateManager.onCheckForUpdate(function(res) {
				  // 请求完新版本信息的回调
				  if (res.hasUpdate) {
				    // 有新版本更新时，弹出提示框
				    uni.showModal({
				      title: '更新提示',
				      content: '检测到新版本，是否更新？',
				      success(res) {
				        if (res.confirm) {
				          // 用户点击了确定，开始下载新版本
				          updateManager.onUpdateReady(function(res) {
				            // 新版本下载完成后的回调
				            uni.showModal({
				              title: '更新提示',
				              content: '新版本已准备好，是否重启应用？',
				              success(res) {
				                if (res.confirm) {
				                  // 用户点击了确定，小程序重启
				                  updateManager.applyUpdate();
				                }
				              }
				            });
				          });
				          // 开始下载新版本
				          updateManager.downloadUpdate();
				        } else if (res.cancel) {
				          // 用户点击了取消，不进行更新
				        }
				      }
				    });
				  }
				});
			},
			//判断是否完善身份信息
			checkinfo(){
				if(uni.getStorageSync('userId')){
					this.$u.post('index/checkbasic',{}).then(res =>{	
						if(res.data.status==1){
							this.checkdingdan()
						}else{
							this.show = true		
						}
										
						
					})
				}
			},
			qdinfo(){
				if(this.confirm_text == "去完善"){
					uni.navigateTo({
						url:'/pages/personal/personal'
					})
					return
				}
				if(this.confirm_text == "去确定"){
					uni.navigateTo({
						url:'/pages/user/order/order?user_status=2'
					})
					return
				}
				
			},
			//判断是否有待确认的订单
			checkdingdan(){	
					this.$u.post('index/check_order',{}).then(res =>{
						//console.log(res)
						if(res.data.count == 0){
							//console.log('已确认')
							return
						}
						//console.log('去确认')
						this.content = "您有  "+res.data.count+"  个待确认订单，请尽快确认"
						this.confirm_text = "去确定"
						this.show = true							
						
					})
			},
			clcikNavigator(item,index){
				if(uni.getStorageSync('userId')){
					uni.navigateTo({
						url:'/pages/user/register/register?id='+item.id+'&index='+index+'&name='+item.name
					})
				}else{
					uni.navigateTo({
						url:'../../login/login'
					})
				}
				
			},
			//  获取轮播图接口
			getBannerList(){
				this.$u.post('/index/images',{}).then(res =>{
					// console.log(res)
					res.data.forEach(item =>{
						item.image = this.$IMG_URL+item.image
					})
					this.list = res.data
				})
			},
			//  获取保修类目
			getClassi(){
				this.$u.post('/index/classi',{}).then(res =>{
					res.data.forEach(item =>{
						item.logo_image = this.$IMG_URL+item.logo_image
						item.child.forEach(items =>{
								items.logo_image = this.$IMG_URL+items.logo_image
						})
					})
					// console.log(res)
					this.leumuList = res.data
				})
			},
			
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if(index == this.current) return ;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if(this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({size: true},res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if(!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return ;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
			
		}
	}
</script>

<style lang="less">
	.u-tabbar__content{
		height: 69rpx;
	}
.u-tabbar__content__item__text{
		font-size: 39rpx !important;
	}
.u-icon__img{
		width: 72rpx !important;
		height: 63rpx !important;
	}
.leimu{
	margin-top: 49rpx;
}
.leimu-title{
	font-size: 50rpx;
	font-weight: bold;
	padding-left: 21rpx;
	position: relative;
	display: flex;
	align-items: center;
	margin-bottom: 39rpx;
	&::after{
		content: "";
		width: 10rpx;
		height: 30rpx;
		background: #0055B0;
		position: absolute;
		left: 0;
	}
}
.leimu-list{
	display: flex;
	flex-wrap: wrap;
	background: #fff;
	border-radius: 10rpx;
	padding: 40rpx 20rpx;
}
.leimu-li{
	width: 110rpx;
	margin-bottom: 59rpx;
	margin-right: 77rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	&:nth-child(4n){
		margin-right: 0;
	}
	// &:nth-last-child(1),
	// &:nth-last-child(2),
	// &:nth-last-child(3),
	// &:nth-last-child(4){
	// 	margin-bottom: 0;
	// }
	image{
		width: 110rpx;
		height: 110rpx;
		border-radius: 20rpx;
		margin-bottom: 25rpx;
	}
	text{
		color: #666666;
		font-size: 26rpx;
	}
}




</style>


<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
		margin: 0 -20rpx;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		width: 200rpx;
		height: 110rpx;
		background: #fff;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		// justify-content: center;
		font-size: 40rpx;
		color: #484747;
		font-weight: 500;
		line-height: 1;
		padding-left: 20rpx;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #0055B0;
		font-size: 40rpx;
		font-weight: 500;
		background: #F9F9F9;
	}
	
	// .u-tab-item-active::before {
	// 	content: "";
	// 	position: absolute;
	// 	border-left: 4px solid $u-type-primary;
	// 	height: 32rpx;
	// 	left: 0;
	// 	top: 39rpx;
	// }

	.u-tab-view {
		height: 100%;
	}
	
	.right-box {
		background-color: rgb(250, 250, 250);
	}
	
	.page-view {
		padding: 16rpx;
	}
	
	.class-item {
		// margin-bottom: 30rpx;
		// background-color: #fff;
		// padding: 16rpx;
		// border-radius: 8rpx;
	}
	
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}
	
	.item-menu-name {
		font-weight: normal;
		font-size: 23px;
		color: #7C7C7C;
	}
	
	.item-container {
		display: flex;
		flex-wrap: wrap;
	}
	
	.thumb-box {
		width: 48%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-bottom: 20rpx;
		background: #fff;
		padding: 30rpx 0;
	}
	
	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
		margin-bottom: 11rpx;
	}
</style>
